package com.example.java.base.netty.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * WsServer:WsServer
 *
 * @author zhangxiaoxiang
 * @date 2020/12/7
 */
public class WsServer {

	public static void main(String[] args) throws Exception {
		// 定义一对线程组
		EventLoopGroup mainGroup = new NioEventLoopGroup();
		EventLoopGroup subGroup = new NioEventLoopGroup();
		try {
			// netty服务器的创建, ServerBootstrap 是一个启动类
			ServerBootstrap server = new ServerBootstrap();
			server.group(mainGroup, subGroup)
				.channel(NioServerSocketChannel.class)
				.childHandler(new WsServerInitializer());
			// 启动server,并且设置8088为启动的端口号,同时启动方式为同步
			ChannelFuture future = server.bind(8088).sync();
			System.out.println("启动成功,然后运行index.html,并打开F12调试查看整个流程,并观察控制台,关闭浏览器再查看控制台");
			// 监听关闭的channel,设置位同步方式
			future.channel().closeFuture().sync();
		} finally {
			mainGroup.shutdownGracefully();
			subGroup.shutdownGracefully();
		}
	}
	
}
